<?php

if (!defined('SYSPATH')) exit("No direct script access allowed!");

/*****************************************************************
 * Define autoload function to load the CL core libraries
 */
function __autoload($name) {
    require_once SYSPATH . 'libraries/' . $name . '.php';
}

/*****************************************************************
 * Load the Common functions file
 */
require_once SYSPATH . 'codelite/Common.php';

set_error_handler('error_handler');

/*****************************************************************
 * Load some classes
 */
$uri = CL_URI::get_instance();
$router = CL_Router::get_instance();
$out = CL_Output::get_instance();


/*****************************************************************
 * Retrieve class and method
 */
$class = $router->fetch_class();
$method = $router->fetch_method();
$parameters = $router->fetch_parameters();

if (!controller_exists($class, $method)) {
    show_error("Requested controller <b>$class</b> does not exist.", "Controller Error");
}

require_once APPPATH . 'controllers/' . $class . '.php' ;

if (!controller_is_callable($class, $method)) {
    show_error("Requested controller <b>$class/$method</b> is not callable. Does the method <b>$method</b> exist?", "Controller Error");
}

$controller = new $class();

call_user_func_array(array($controller, $method), $parameters);

$controller->end();

$out->display();


// HOW ABOUT CLOSING DB CONNECTIONS???
// HOW ABOUT TESTING BEHAVIOUR OF THE PAGE WITH INCORRECT SETTINGS!!!

/* End of file CodeLite.php */
/* Location: /system/codelite/CodeLite.php */

